/*
This file is part of Cafca.j

Cafca is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Cafca is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Cafca.  If not, see <http://www.gnu.org/licenses/>.

If you have any comment, suggestion or idea, do not hesitate to contact me at shenrotin@gmail.com
*/

package org.crag.cafca.gui.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.crag.cafca.db.SqlConnector;
import org.crag.cafca.gui.MenuManager;
import org.crag.cafca.util.Constants;

/**
 * Servlet implementation class Initializer
 */
@WebServlet("/Initializer")
public class Initializer extends HttpServlet 
{
	/**
	 * The log4J logger. See http://logging.apache.org/log4j/1.2/index.html for more info.
	 */
	private static Logger log=Logger.getLogger(new Throwable().getStackTrace()[0].getClassName());
       
    public Initializer() 
    {
        super();
    }

    @Override
    public void init(ServletConfig config) throws ServletException 
    {
    	log.info("Initializer called");
    	
    	super.init(config);
    	
    	//Initialization
    	Constants.projectPath=getServletContext().getRealPath("/");
    	
    	String file = config.getInitParameter(Constants.INIT_LOG4J_INIT_FILE); 
    	
    	// if the log4j-init-file is not set
    	if(file != null) 
    	{
    		DOMConfigurator.configure(Constants.projectPath+file);
    	}
    	
    	//Initialize the connection to the database
    	String dbHost=config.getInitParameter(Constants.INIT_DB_HOST);
    	String dbPort=config.getInitParameter(Constants.INIT_DB_PORT);
    	String database=config.getInitParameter(Constants.INIT_DATABASE);
    	String user=config.getInitParameter(Constants.INIT_DB_USER);
    	String password=config.getInitParameter(Constants.INIT_DB_PASSWORD);
    	SqlConnector.init(dbHost,dbPort, database, user, password);
    	
    	MenuManager systemManager=MenuManager.getMenuManager();
    	systemManager.loadMenuFromXml(Constants.projectPath+config.getInitParameter(Constants.INIT_MENU_FILE));
    }	

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{}

	@Override
	public void destroy() 
	{
		super.destroy();
		SqlConnector.destroy();
	}
}
